﻿using Dapper;
using ML.Entity;
using ML.IRepository;
using System.Linq;

namespace ML.Repository
{
    /// <summary>
    /// 汇率仓储实现类
    /// </summary>
    public class CurrencyRepository : DBFactory, ICurrencyRepository
    {
        public Pager<CurrencyResponse> List(CurrencySearch currencySearch)
        {
            //组装调用存储过程参数
            var criteria = new PageCriteria() { TableName = "ML_CURRENCY" };
            if (!string.IsNullOrEmpty(currencySearch.name_search) && currencySearch.name_search != "-1")
            {
                criteria.Conditions += string.Format(" AND Name='{0}'", currencySearch.name_search);
            }

            criteria.PageIndex = currencySearch.pageindex;
            criteria.Field = @"*";
            criteria.PageSize = currencySearch.pagesize;
            criteria.SortName = currencySearch.ordername;
            if (currencySearch.orderby)
            {
                criteria.Sort = "DESC";
            }
            else
            {
                criteria.Sort = "ASC";
            }

            //调用存储过程进行查询并且返回
            var list = GetPageData<CurrencyResponse>(criteria);
            return list;
        }

        public CurrencyEntity GetLastOne()
        {
            const string sql = @"SELECT  * FROM ml_currency WHERE `Name`='100' ORDER BY Id DESC limit 1";
            using (_db)
            {
                return _db.Query<CurrencyEntity>(sql).FirstOrDefault();
            }
        }

        public CurrencyEntity GetById(string id)
        {
            const string sql = @"SELECT  * FROM ml_currency WHERE Id=@Id";
            using (_db)
            {
                return _db.Query<CurrencyEntity>(sql, new { @Id = id }).FirstOrDefault();
            }
        }

        public bool Add(CurrencyEntity entity)
        {
            throw new System.NotImplementedException();
        }

        public bool Update(CurrencyEntity entity)
        {
            throw new System.NotImplementedException();
        }

        public bool Delete(CurrencyEntity entity)
        {
            throw new System.NotImplementedException();
        }

        public bool Delete(string id)
        {
            throw new System.NotImplementedException();
        }
    }
}